home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / prog / pclcjs.arj / DIRSELF.C < prev    next >
C/C++ Source or Header  |  1992-10-01  |  17KB  |  482 lines

  1. #include "STDIO.h"
  2. #include "STDLIB.h"
  3. #include "IO.h"
  4. #include "FCNTL.h"
  5. #include "DOS.h"
  6. #include "DIR.h"
  7.  
  8. /*
  9.     DIRSEL.c   v1.00    Vernon E. Davis
  10.     07/12/87            17 South Centre Street
  11.                         Merchantville, NJ 08109
  12.                         CompuServe [71330,2705]
  13.                         GEnie - VED
  14.  
  15.                         Adapted for Power C 2.0.1 by
  16.                         Chuck Steenburgh [72330,1776]
  17.  
  18.  
  19.     purpose   : to select one filename from the list presented
  20.     syntax    : #include "STDIO.h"
  21.               : #include "STDLIB.h"
  22.               : #include "IO.h"
  23.               : #include "FCNTL.h"
  24.               : #include "DOS.h"
  25.               : #include "DIR.h"
  26.               : *p = dirselect(*mask,attr);
  27.               : char *p;     the filename
  28.               : char *mask;  the directory mask, i.e. "*.*"
  29.               : int attr;    the attribute to look for,i.e.
  30.               :               FA_RDONLY, FA_HIDDEN, FA_SYSTEM,
  31.               :               FA_LABEL,  FA_DIREC,  FA_ARCH.
  32.               :               (see dos.h or Turbo C manual for info.)
  33.               :
  34.     returns   : either a pointer to a filename that was selected or
  35.               : NULL if Escape was pressed and if no file is found.
  36.               :
  37.     descrip.  : a call to dirselect() first determines if there are any files
  38.               : that meet the specs received from the directory mask. If
  39.               : no files were found, the function immediately returns NULL.
  40.               : Otherwise, all filenames that meet the mask spec are copyed
  41.               : into an array that can hold up to 120 selections. The routine
  42.               : then clears a portion of the screen, saving the contents of
  43.               : the screen in an array. A frame is drawn in the cleared portion
  44.               : of the screen and the current directory contents are displayed
  45.               : in the boxed area as per the  mask that was received. The first
  46.               : selection is highlighted in reverse video. The up/down/left/
  47.               : right arrow keys will move the highlight around.  The Home Key
  48.               : returns to the first selection. The End key moves to the last
  49.               : selection. When < CR > is pressed,  the screen returns to
  50.               : normal, the cursor is placed in its original position and the
  51.               : value returned from the function is a pointer to a string
  52.               : containing the filename selected.  If Esc was pressed instead,
  53.               : the previous sequence is performed, with the exception of NULL
  54.               : being returned instead of the filename.
  55.               :
  56.     notes     : A. All routines in this file can be run under TC, the
  57.               :    Turbo C Integrated Development Environment. Because of
  58.               :    this, the routine will run slow when executed. It was
  59.               :    not designed for speed.
  60.               : B. These routines rely on the video hardware interrupt 0x10
  61.               :    for the cursor information. Insure that the PC is an IBM
  62.               :    or compatable.
  63.               : C. Three (3) cursor functions and a frame drawing function are
  64.               :    available. They are as follows:
  65.               :    1. void     gotoxy(x,y); move cursor to new location
  66.               :                int x (0..79)
  67.               :                int y (0..24)
  68.               :    2. void     cursor(ON/OFF); turn cursor on or off
  69.               :                int ON (1) or OFF (0)
  70.               :    3. xyposdef wherecur(void); locate cursor position
  71.               :                xyposdef.x (0..79)  column location
  72.               :                xyposdef.y (0..24)  row location
  73.               :    4. void     drframe(top,left,bottom,right); draw double fr.
  74.               :                int top    (0..24)  top corner
  75.               :                int left   (0..79)  left corner
  76.               :                int bottom (0..24)  bottom corner
  77.               :                int right  (0..79)  right corner
  78.               :    The other functions declared are special to dirselect()
  79.               :    and of no real use; however, they are documented.
  80. */
  81.  
  82. #define TRUE 1
  83. #define FALSE 0
  84. #define ON 1
  85. #define OFF 0
  86. #define VIDINTR 0x10
  87. #define EQUINTR 0x11
  88. #define FPOS1  1
  89. #define FPOS2 14
  90. #define FPOS3 27
  91. #define FPOS4 40
  92. #define FPOS5 53
  93. #define FPOS6 66
  94.  
  95.    typedef struct     /* structure for directory array */
  96.    {  char fname[13];
  97.    }  dirdef;
  98.  
  99.    typedef struct     /* structure for cursor column(x) and row(y) */
  100.    {  int x,y;
  101.    }  xyposdef;
  102.  
  103. /*  function declarations  */
  104.  
  105. char* dirselect(char*,int);                   /* the main function */
  106. void hilite(unsigned char,unsigned char);     /* highlights the filename */
  107. xyposdef fnamepos(unsigned char);             /* X/Y position of filename */
  108. void gotoxy(unsigned char,unsigned char);     /* move cursor to new location */
  109. void cursor(int);                             /* turn cursor on or off */
  110. xyposdef wherecur(void);                      /* current cursor location */
  111. int menubox(int r1, c1, r2, c2, color, boxtype, shadow, shade);  /* See file MENUBOX.C */
  112.  
  113. char* dirselect(mask,attr)
  114.    char mask[13];
  115.    int attr;
  116. {  union REGS regs;
  117.    struct ffblk ffblk;
  118.    int h,i,j,k,row,error,oldcurx,oldcury;
  119.    unsigned char oldpos,newpos;
  120.    char chrbuf[4000],atrbuf[4000];
  121.    char key;
  122.    dirdef dos_dir[121],*dos_dirptr;
  123.    xyposdef xypos;
  124.    h=0;
  125.    if((error=findfirst(mask,&ffblk,attr))==-1) /* if not found, return NULL */
  126.       return(NULL);
  127.    while(!error&&h!=120)        /* else, collect filenames */
  128.    {  dos_dirptr=&dos_dir[h++];
  129.       strcpy(dos_dirptr->fname,ffblk.ff_name);
  130.       error=findnext(&ffblk);
  131.    };
  132.    xypos=wherecur();     /* store old cursor position */
  133.    oldcurx=xypos.x;
  134.    oldcury=xypos.y;
  135.    cursor(OFF);
  136.    k=0;
  137.    for(i=0;i<((h/6)+3);i++)   /* store old screen info & clear portion */
  138.    {  for(j=0;j<80;j++)
  139.       {  gotoxy(j,i);
  140.          regs.h.bh=0;
  141.          regs.h.ah=8;
  142.          int86(VIDINTR,®s,®s);
  143.          chrbuf[k]=regs.h.al;
  144.          atrbuf[k]=regs.h.ah;
  145.          putch(32);
  146.          k++;
  147.       };
  148.    };
  149.    gotoxy(0,0);
  150.    i=0; j=0; row=1;
  151.    while(TRUE)                  /* display all filenames */
  152.    {  dos_dirptr=&dos_dir[i];
  153.       xypos=fnamepos(i);
  154.       gotoxy(xypos.x,xypos.y);
  155.       printf("%-012.12s",dos_dirptr->fname);
  156.       i++; j++;
  157.       if(i==h||i==120) break;
  158.       if(j>5)
  159.       {  j=0;
  160.          row++;
  161.          printf("\n");
  162.       };
  163.    };
  164.    menubox(0,0,(row+1),79,15,3,0,1);  /* draw the frame */
  165.    hilite(255,0);            /* highlight the first filename */
  166.    oldpos=0;
  167.    newpos=0;
  168.    while(TRUE)           /* get keypress and do appropriate action */
  169.    {  key=getch();
  170.       switch(key)
  171.       {  case 27:  /* Esc  */
  172.             k=0;
  173.             for(i=0;i<((h/6)+3);i++)  /* return contents of old screen */
  174.             {  for(j=0;j<80;j++)
  175.                {  gotoxy(j,i);
  176.                   regs.h.al=chrbuf[k];
  177.                   regs.h.bl=atrbuf[k];
  178.                   regs.h.ch=0;
  179.                   regs.h.cl=1;
  180.                   regs.h.bh=0;
  181.                   regs.h.ah=9;
  182.                   int86(VIDINTR,®s,®s);
  183.                   k++;
  184.                };
  185.             };
  186.             gotoxy(oldcurx,oldcury);         /* return old cursor position */
  187.             cursor(ON);
  188.             return(NULL);                    /* return NULL */
  189.          case 71:  /* Home */                /* goto first filename */
  190.             oldpos=newpos;
  191.             newpos=0;
  192.             hilite(oldpos,newpos);
  193.             break;
  194.          case 79:  /* End  */                /* goto last filename */
  195.             oldpos=newpos;
  196.             newpos=(h-1);
  197.             hilite(oldpos,newpos);
  198.             break;
  199.          case 72:  /* Up   */                /* move up one filename */
  200.             i=newpos;
  201.             i-=6;
  202.             if(i>=0)
  203.             {  oldpos=newpos;
  204.                newpos=i;
  205.                hilite(oldpos,newpos);
  206.             };
  207.             break;
  208.          case 80:  /* Down */                /* move down one filename */
  209.             i=newpos;
  210.             i+=6;
  211.             if(i<h)
  212.             {  oldpos=newpos;
  213.                newpos=i;
  214.                hilite(oldpos,newpos);
  215.             };
  216.             break;
  217.          case 75:  /* Left */                /* move left one filename */
  218.             i=newpos;
  219.             i--;
  220.             if(i>=0)
  221.             {  oldpos=newpos;
  222.                newpos=i;
  223.                hilite(oldpos,newpos);
  224.             };
  225.             break;
  226.          case 77:  /* Right */               /* move right one filename */
  227.             i=newpos;
  228.             i++;
  229.             if(i<h)
  230.             {  oldpos=newpos;
  231.                newpos=i;
  232.                hilite(oldpos,newpos);
  233.             };
  234.             break;
  235.          case 13:  /* CR */
  236.             k=0;
  237.             for(i=0;i<((h/6)+3);i++)       /* return contents of old screen */
  238.             {  for(j=0;j<80;j++)
  239.                {  gotoxy(j,i);
  240.                   regs.h.al=chrbuf[k];
  241.                   regs.h.bl=atrbuf[k];
  242.                   regs.h.ch=0;
  243.                   regs.h.cl=1;
  244.                   regs.h.bh=0;
  245.                   regs.h.ah=9;
  246.                   int86(VIDINTR,®s,®s);
  247.                   k++;
  248.                };
  249.             };
  250.             gotoxy(oldcurx,oldcury);    /* return old cursor position */
  251.             cursor(ON);
  252.             return(dos_dir[newpos].fname);   /* return with filename */
  253.       };
  254.    };
  255. }
  256.  
  257. void hilite(old,new)         /* highlight a filename on the screen */
  258.    unsigned char old,new;
  259. {  union REGS regs;
  260.    int oldx,oldy,newx,newy,i;
  261.    unsigned char ccolor,locolor,hicolor;
  262.    xyposdef xypos;
  263.    xypos=fnamepos(old);
  264.    oldx=xypos.x;
  265.    oldy=xypos.y;
  266.    xypos=fnamepos(new);    /* get the position in the array of the filename */
  267.    newx=xypos.x;
  268.    newy=xypos.y;
  269.    for(i=0;i<12;i++)
  270.    {  if(old<121)       /* if valid position, reverse video, old selection */
  271.       {  gotoxy((oldx+i),oldy);
  272.          regs.h.bh=0;
  273.          regs.h.ah=8;
  274.          int86(VIDINTR,®s,®s);
  275.          ccolor=regs.h.ah;
  276.          locolor=ccolor&0x0F;
  277.          locolor<<=4;
  278.          hicolor=ccolor&0xF0;
  279.          hicolor>>=4;
  280.          ccolor=locolor+hicolor;
  281.          regs.h.ch=0;
  282.          regs.h.cl=1;
  283.          regs.h.bh=0;
  284.          regs.h.bl=ccolor;
  285.          regs.h.ah=9;
  286.          int86(VIDINTR,®s,®s);
  287.       };
  288.       gotoxy((newx+i),newy);         /* reverse video, new selection */
  289.       regs.h.bh=0;
  290.       regs.h.ah=8;
  291.       int86(VIDINTR,®s,®s);
  292.       ccolor=regs.h.ah;
  293.       locolor=ccolor&0x0F;
  294.       locolor<<=4;
  295.       hicolor=ccolor&0xF0;
  296.       hicolor>>=4;
  297.       ccolor=locolor+hicolor;
  298.       regs.h.ch=0;
  299.       regs.h.cl=1;
  300.       regs.h.bh=0;
  301.       regs.h.bl=ccolor;
  302.       regs.h.ah=9;
  303.       int86(VIDINTR,®s,®s);
  304.    };
  305.    return;
  306. }
  307.  
  308. xyposdef fnamepos(arypos)   /* determine position on screen of filename[] */
  309.    unsigned char arypos;
  310. {  int x,y;
  311.    xyposdef xyp;
  312.    switch(arypos)
  313.    {  case   0: x=FPOS1; y= 1; break;   /* see #define for x coordinates */
  314.       case   1: x=FPOS2; y= 1; break;
  315.       case   2: x=FPOS3; y= 1; break;
  316.       case   3: x=FPOS4; y= 1; break;
  317.       case   4: x=FPOS5; y= 1; break;
  318.       case   5: x=FPOS6; y= 1; break;
  319.       case   6: x=FPOS1; y= 2; break;
  320.       case   7: x=FPOS2; y= 2; break;
  321.       case   8: x=FPOS3; y= 2; break;
  322.       case   9: x=FPOS4; y= 2; break;
  323.       case  10: x=FPOS5; y= 2; break;
  324.       case  11: x=FPOS6; y= 2; break;
  325.       case  12: x=FPOS1; y= 3; break;
  326.       case  13: x=FPOS2; y= 3; break;
  327.       case  14: x=FPOS3; y= 3; break;
  328.       case  15: x=FPOS4; y= 3; break;
  329.       case  16: x=FPOS5; y= 3; break;
  330.       case  17: x=FPOS6; y= 3; break;
  331.       case  18: x=FPOS1; y= 4; break;
  332.       case  19: x=FPOS2; y= 4; break;
  333.       case  20: x=FPOS3; y= 4; break;
  334.       case  21: x=FPOS4; y= 4; break;
  335.       case  22: x=FPOS5; y= 4; break;
  336.       case  23: x=FPOS6; y= 4; break;
  337.       case  24: x=FPOS1; y= 5; break;
  338.       case  25: x=FPOS2; y= 5; break;
  339.       case  26: x=FPOS3; y= 5; break;
  340.       case  27: x=FPOS4; y= 5; break;
  341.       case  28: x=FPOS5; y= 5; break;
  342.       case  29: x=FPOS6; y= 5; break;
  343.       case  30: x=FPOS1; y= 6; break;
  344.       case  31: x=FPOS2; y= 6; break;
  345.       case  32: x=FPOS3; y= 6; break;
  346.       case  33: x=FPOS4; y= 6; break;
  347.       case  34: x=FPOS5; y= 6; break;
  348.       case  35: x=FPOS6; y= 6; break;
  349.       case  36: x=FPOS1; y= 7; break;
  350.       case  37: x=FPOS2; y= 7; break;
  351.       case  38: x=FPOS3; y= 7; break;
  352.       case  39: x=FPOS4; y= 7; break;
  353.       case  40: x=FPOS5; y= 7; break;
  354.       case  41: x=FPOS6; y= 7; break;
  355.       case  42: x=FPOS1; y= 8; break;
  356.       case  43: x=FPOS2; y= 8; break;
  357.       case  44: x=FPOS3; y= 8; break;
  358.       case  45: x=FPOS4; y= 8; break;
  359.       case  46: x=FPOS5; y= 8; break;
  360.       case  47: x=FPOS6; y= 8; break;
  361.       case  48: x=FPOS1; y= 9; break;
  362.       case  49: x=FPOS2; y= 9; break;
  363.       case  50: x=FPOS3; y= 9; break;
  364.       case  51: x=FPOS4; y= 9; break;
  365.       case  52: x=FPOS5; y= 9; break;
  366.       case  53: x=FPOS6; y= 9; break;
  367.       case  54: x=FPOS1; y=10; break;
  368.       case  55: x=FPOS2; y=10; break;
  369.       case  56: x=FPOS3; y=10; break;
  370.       case  57: x=FPOS4; y=10; break;
  371.       case  58: x=FPOS5; y=10; break;
  372.       case  59: x=FPOS6; y=10; break;
  373.       case  60: x=FPOS1; y=11; break;
  374.       case  61: x=FPOS2; y=11; break;
  375.       case  62: x=FPOS3; y=11; break;
  376.       case  63: x=FPOS4; y=11; break;
  377.       case  64: x=FPOS5; y=11; break;
  378.       case  65: x=FPOS6; y=11; break;
  379.       case  66: x=FPOS1; y=12; break;
  380.       case  67: x=FPOS2; y=12; break;
  381.       case  68: x=FPOS3; y=12; break;
  382.       case  69: x=FPOS4; y=12; break;
  383.       case  70: x=FPOS5; y=12; break;
  384.       case  71: x=FPOS6; y=12; break;
  385.       case  72: x=FPOS1; y=13; break;
  386.       case  73: x=FPOS2; y=13; break;
  387.       case  74: x=FPOS3; y=13; break;
  388.       case  75: x=FPOS4; y=13; break;
  389.       case  76: x=FPOS5; y=13; break;
  390.       case  77: x=FPOS6; y=13; break;
  391.       case  78: x=FPOS1; y=14; break;
  392.       case  79: x=FPOS2; y=14; break;
  393.       case  80: x=FPOS3; y=14; break;
  394.       case  81: x=FPOS4; y=14; break;
  395.       case  82: x=FPOS5; y=14; break;
  396.       case  83: x=FPOS6; y=14; break;
  397.       case  84: x=FPOS1; y=15; break;
  398.       case  85: x=FPOS2; y=15; break;
  399.       case  86: x=FPOS3; y=15; break;
  400.       case  87: x=FPOS4; y=15; break;
  401.       case  88: x=FPOS5; y=15; break;
  402.       case  89: x=FPOS6; y=15; break;
  403.       case  90: x=FPOS1; y=16; break;
  404.       case  91: x=FPOS2; y=16; break;
  405.       case  92: x=FPOS3; y=16; break;
  406.       case  93: x=FPOS4; y=16; break;
  407.       case  94: x=FPOS5; y=16; break;
  408.       case  95: x=FPOS6; y=16; break;
  409.       case  96: x=FPOS1; y=17; break;
  410.       case  97: x=FPOS2; y=17; break;
  411.       case  98: x=FPOS3; y=17; break;
  412.       case  99: x=FPOS4; y=17; break;
  413.       case 100: x=FPOS5; y=17; break;
  414.       case 101: x=FPOS6; y=17; break;
  415.       case 102: x=FPOS1; y=18; break;
  416.       case 103: x=FPOS2; y=18; break;
  417.       case 104: x=FPOS3; y=18; break;
  418.       case 105: x=FPOS4; y=18; break;
  419.       case 106: x=FPOS5; y=18; break;
  420.       case 107: x=FPOS6; y=18; break;
  421.       case 108: x=FPOS1; y=19; break;
  422.       case 109: x=FPOS2; y=19; break;
  423.       case 110: x=FPOS3; y=19; break;
  424.       case 111: x=FPOS4; y=19; break;
  425.       case 112: x=FPOS5; y=19; break;
  426.       case 113: x=FPOS6; y=19; break;
  427.       case 114: x=FPOS1; y=20; break;
  428.       case 115: x=FPOS2; y=20; break;
  429.       case 116: x=FPOS3; y=20; break;
  430.       case 117: x=FPOS4; y=20; break;
  431.       case 118: x=FPOS5; y=20; break;
  432.       case 119: x=FPOS6; y=20; break;
  433.    };
  434.    xyp.x=x; xyp.y=y;
  435.    return(xyp);      /* return with X/Y coordinates of filename[] */
  436. }
  437.  
  438.  
  439. void cursor(mode)  /* turn the cursor on or off */
  440.    int mode;
  441. {  union REGS regs;
  442.    int i;
  443.    if(mode==OFF)
  444.    {  regs.h.cl=7;
  445.       regs.h.ch=32;    /* set bit 5 high to turn cursor off */
  446.    }
  447.    else
  448.    {  int86(EQUINTR,®s,®s);  /* get equipment to determine video mode */
  449.       i=regs.h.al;
  450.       regs.x.cx=0x0607;    /* prepare for CGA */
  451.       if((i&=0x10)!=NULL)  /* if MA/MGA */
  452.         regs.x.cx=0x0B0C;  /* prepare for MA/MGA */
  453.    };
  454.    regs.h.ah=1;
  455.    int86(VIDINTR,®s,®s);
  456.    return;
  457. }
  458.  
  459. void gotoxy(x,y)  /* move cursor to a new location */
  460.    unsigned char x,y;
  461. {  union REGS regs;
  462.    regs.h.bh=0;
  463.    regs.h.dl=x;
  464.    regs.h.dh=y;
  465.    regs.h.ah=2;
  466.    int86(VIDINTR,®s,®s);
  467.    return;
  468. }
  469.  
  470. xyposdef wherecur(void)  /* report current cursor location */
  471. {  union REGS regs;
  472.    xyposdef xyp;
  473.    regs.h.ah=3;
  474.    regs.h.bh=0;
  475.    int86(VIDINTR,®s,®s);
  476.    xyp.y=regs.h.dh;
  477.    xyp.x=regs.h.dl;
  478.    return(xyp);
  479. }
  480.  
  481. /*  End Of Function  dirselect()  */
  482.